home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libiconv_src.lha / src / euc_tw.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-07  |  2.1 KB  |  93 lines

  1.  
  2. /*
  3.  * EUC-TW
  4.  */
  5.  
  6. static int
  7. euc_tw_mbtowc (conv_t conv, wchar_t *pwc, const unsigned char *s, int n)
  8. {
  9.   unsigned char c = *s;
  10.   /* Code set 0 (ASCII) */
  11.   if (c < 0x80)
  12.     return ascii_mbtowc(conv,pwc,s,n);
  13.   /* Code set 1 (CNS 11643-1992 Plane 1) */
  14.   if (c >= 0xa1 && c < 0xff) {
  15.     if (n < 2)
  16.       return RET_TOOFEW(0);
  17.     {
  18.       unsigned char c2 = s[1];
  19.       if (c2 >= 0xa1 && c2 < 0xff) {
  20.         unsigned char buf[2];
  21.         buf[0] = c-0x80; buf[1] = c2-0x80;
  22.         return cns11643_1_mbtowc(conv,pwc,buf,2);
  23.       } else
  24.         return RET_ILSEQ;
  25.     }
  26.   }
  27.   /* Code set 2 (CNS 11643-1992 Planes 1-16) */
  28.   if (c == 0x8e) {
  29.     if (n < 4)
  30.       return RET_TOOFEW(0);
  31.     {
  32.       unsigned char c2 = s[1];
  33.       if (c2 >= 0xa1 && c2 <= 0xb0) {
  34.         unsigned char c3 = s[2];
  35.         unsigned char c4 = s[3];
  36.         if (c3 >= 0xa1 && c3 < 0xff && c4 >= 0xa1 && c4 < 0xff) {
  37.           unsigned char buf[2];
  38.           int ret;
  39.           buf[0] = c3-0x80; buf[1] = c4-0x80;
  40.           switch (c2-0xa0) {
  41.             case 1: ret = cns11643_1_mbtowc(conv,pwc,buf,2); break;
  42.             case 2: ret = cns11643_2_mbtowc(conv,pwc,buf,2); break;
  43.             case 3: ret = cns11643_3_mbtowc(conv,pwc,buf,2); break;
  44.             default: return RET_ILSEQ;
  45.           }
  46.           if (ret == RET_ILSEQ)
  47.             return RET_ILSEQ;
  48.           if (ret != 2) abort();
  49.           return 4;
  50.         }
  51.       }
  52.     }
  53.   }
  54.   return RET_ILSEQ;
  55. }
  56.  
  57. static int
  58. euc_tw_wctomb (conv_t conv, unsigned char *r, wchar_t wc, int n)
  59. {
  60.   unsigned char buf[3];
  61.   int ret;
  62.  
  63.   /* Code set 0 (ASCII) */
  64.   ret = ascii_wctomb(conv,r,wc,n);
  65.   if (ret != RET_ILSEQ)
  66.     return ret;
  67.  
  68.   ret = cns11643_wctomb(conv,buf,wc,3);
  69.   if (ret != RET_ILSEQ) {
  70.     if (ret != 3) abort();
  71.  
  72.     /* Code set 1 (CNS 11643-1992 Plane 1) */
  73.     if (buf[0] == 0) {
  74.       if (n < 2)
  75.         return RET_TOOSMALL;
  76.       r[0] = buf[1]+0x80;
  77.       r[1] = buf[2]+0x80;
  78.       return 2;
  79.     }
  80.  
  81.     /* Code set 2 (CNS 11643-1992 Planes 1-16) */
  82.     if (n < 4)
  83.       return RET_TOOSMALL;
  84.     r[0] = 0x8e;
  85.     r[1] = buf[0]+0xa1;
  86.     r[2] = buf[1]+0x80;
  87.     r[3] = buf[2]+0x80;
  88.     return 4;
  89.   }
  90.  
  91.   return RET_ILSEQ;
  92. }
  93.